import LogUtil from "./LogUtil";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export class Utils {
    /**
     * 判断一个字段是否为空
     * @param x 
     */
    static isEmpty(x: any): boolean {
        if (Array.isArray(x) || typeof x === 'string' || x instanceof String) {
            return x.length === 0;
        }
        if (x instanceof Map || x instanceof Set) {
            return x.size === 0;
        }
        if (({}).toString.call(x) === '[object Object]') {
            return Object.keys(x).length === 0;
        }
        if (x && x.constructor === RegExp) {
            return true;
        }
        return false;
    }

    /**
     * 随机给定区间内整数
     * @param min 最小整数
     * @param max 最大整数
     * @returns 
     */
    static getRandomInt(min: number, max: number): number {
        if (min >= max) return min;
        return Math.floor(Math.random() * (max - min + 1) + min);
    }

    /**
     * 是否为json数据
     * @param param 
     * @returns 
     */
    static isJson(param: any): boolean {
        try {
            if (typeof param == "string" && typeof JSON.parse(param) == "object") {
                return true;
            }
            if (typeof (param) == "object" && Object.prototype.toString.call(param).toLowerCase() == "[object object]" && !param.length) {
                return true;
            }
        } catch (e) {
        }
        return false;
    }

    /**
     * 简单json对象拷贝
     */
    static JsonDeepCopy(param: any): any {
        if (typeof (param) == "object" && Object.prototype.toString.call(param).toLowerCase() == "[object object]" && !param.length) {
            return JSON.parse(JSON.stringify(param));
        }
        LogUtil.warn('不是json对象无法拷贝', param);
        return {};
    }

    /**
     * json数据解析
     * @param param 
     * @returns 
     */
    static JsonParse(param: string): any {
        if (this.isJson(param)) {
            return JSON.parse(param);
        }
        LogUtil.warn('不是json对象无法解析', param);
        return null;
    }

    /**
     * 时间戳格式化日期显示
     * @param time 时间戳毫秒 1635130950425
     * @param format 日期格式化参数"YM"
     * @returns 
     */
    static formatDate(time: number, format: string): string { //format="YM"
        function formatNumber(n: number | string) {
            n = n.toString();
            return n[1] ? n : '0' + n;
        }
        const formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
        const returnArr = [];
        const date = new Date(time);
        returnArr.push(date.getFullYear());
        returnArr.push(formatNumber(date.getMonth() + 1));
        returnArr.push(formatNumber(date.getDate()));
        returnArr.push(formatNumber(date.getHours()));
        returnArr.push(formatNumber(date.getMinutes()));
        returnArr.push(formatNumber(date.getSeconds()));
        for (const i in returnArr) {
            format = format.replace(formateArr[i], returnArr[i]);
        }
        return format;
    }

    /**
     * 数字转字符串标示法
     * @param {*} t 
     */
    static tani(t: number): string {
        let taniEN = ["", "K", "M", "G", "T"];
        let taniENPow = [4, 7, 10, 13];
        let taniENPow2 = [0, 3, 6, 9, 12];
        for (let i in taniENPow) {
            if (t < Math.pow(10, taniENPow[i])) {
                return Math.floor(t / Math.pow(10, taniENPow2[i])) + taniEN[i]
            }
        }
        return Math.floor(t / Math.pow(10, 12)) + "T"
    }

    static getFileName(path: string): string {
        let startIdx = path.lastIndexOf('/');
        return path.substring(startIdx + 1);
    }

    static getMD5(data: any): string {
        return CryptoJS.MD5(data).toString();
    }

    /**
     * 全局的加速、减速 1正常
     * @param t 
     */
    static setTimeScale(t:number){
        cc.director.getScheduler().setTimeScale(t);
    }

}

//字符长度缩略显示
String.prototype.clamp = function (start: number, threshold: number, suffix: string = '...') {
    if (this.replace(/[^\x00-\xff]/g, 'xx').length <= threshold) return this;
    let charCount = 0;
    let result = '';
    for (let i = start; i < this.length; i++) {
        charCount += /[^\x00-\xff]/.test(this[i]) ? 2 : 1;
        if (charCount > threshold) return result += suffix;
        result += this[i];
    }
    return result;
};

/**
 * 字符串格式化 
 * '第{0}天'.format([1]);
 */
String.prototype.format = function (args: any) {
    let result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (let key in args) {
                if (args[key] != undefined) {
                    let reg = "{" + key + "}"
                    result = result.replace(reg, args[key]);
                }
            }
        } else {
            for (let i = 0; i < arguments.length; ++i) {
                if (arguments[i] != undefined) {
                    let reg = "{" + i + "}"
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}